package com.project.auth;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;

/**
 * @Description
 * @Author : XIAOMING
 * @create 2023/5/30 15:45
 */
@SpringBootTest
public class ProcessTest3 {

    //注入activiti中的RepositotyService用来部署流程
    @Autowired
    private RepositoryService repositoryService;
    //注入activiti中的RuntimeService用来开启流程
    @Autowired
    private RuntimeService runtimeService;
    //注入activiti中的TaskService用来处理流程
    @Autowired
    private TaskService taskService;
    //注入activiti中的HistoryService用来查看历史相关

    @Test
    public void depoloyProcess() {

        //流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/WorkOvertime.bpmn20.xml")
                .name("加班申请流程2")
                .deploy();

        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startProcessInstance() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("assignee1", "tom2");
        map.put("assignee2", "jerry2");

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("WorkOvertime", map);

        System.out.println("流程定义ID: " + processInstance.getProcessDefinitionId());
        System.out.println("流程实例ID: " + processInstance.getId());
        System.out.println("活动流程ID: " + processInstance.getProcessDefinitionName());
    }

    //查询个人代办任务
    @Test
    public void findPendingTaskList() {
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("tom2")
                .list();

        for (Task task : taskList) {
            System.out.println("流程实例ID: " + task.getProcessDefinitionId());
            System.out.println("任务ID: " + task.getId());
            System.out.println("任务负责人: " + task.getAssignee());
            System.out.println("任务名称: " + task.getName());
        }
    }
}
